package cn.neusoft.controller;

import cn.neusoft.domain.Commodity;
import cn.neusoft.domain.MvpSupplier;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.service.MvpSupplierService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;

import java.util.Date;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController // @Controller + @ResponseBody
@RequestMapping(value = "/mvpSupplier",produces = "application/json;charset=utf-8")
//接口文档的注解
@Api(value = "供应商结算信息模块的API",description="供应商结算功能")
public class MvpSupplierController {

  @Autowired
  MvpSupplierService mvpSupplierService;

  @PostMapping("/commoditylist")
  @ApiOperation(value = "获取供应商下商品")
  public ResultBean findCommodity(@RequestParam String supplier_id){
    List<Commodity> commodityList=mvpSupplierService.findSupplierCommodity(supplier_id);
    return ResultBean.success(commodityList);
  }

  @PostMapping("/mvp")
  @ApiOperation(value = "获取结算信息")
  public  ResultBean findMvpInfo(@RequestBody MvpSupplier mvpSupplier){
    String order_commodity_id=mvpSupplier.getMvpsupplier_comodity_id();
    Date startDate=mvpSupplier.getStart_date();
    Date endDate=mvpSupplier.getEnd_date();
    Long supply =mvpSupplierService.findSupplyAmount(order_commodity_id,startDate,endDate);
    Long returnAmount  =mvpSupplierService.findReturnAmount(order_commodity_id,startDate,endDate);
    if(supply==null){
      return ResultBean.nonsupply();
    }
    if (returnAmount==null){
      returnAmount= Long.valueOf(0);}
    Long total=supply-returnAmount;
    Double price=mvpSupplierService.findCommodity(order_commodity_id).getOriginal_price();
    BigDecimal sumamount= BigDecimal.valueOf(price*total);
    mvpSupplier.setCommodity_name(mvpSupplierService.findCommodity(order_commodity_id).getCommodity_name());
    mvpSupplier.setMeasures(mvpSupplierService.findCommodity(order_commodity_id).getMeasure());
    mvpSupplier.setSupply_amount(supply);
    mvpSupplier.setMvpsupplier_comodity_id(order_commodity_id);
    mvpSupplier.setReturn_amount(returnAmount);
    mvpSupplier.setMvpsupplier_amount(total);
    mvpSupplier.setMvpsupplier_cost(sumamount);
    mvpSupplier.setPrice(price);
    mvpSupplier.setStart_date(startDate);
    mvpSupplier.setEnd_date(endDate);
    return ResultBean.success(mvpSupplier);
  }

}
